$(function() {
	$("body").showLoading();
	roleList();
});

// 初始化角色列表
function roleList() {
	$.post(PATH + "/sysRoleAjaxList.do", {
		async : true
	}, function(data) {
		if (data.ret == 1) {
			var html = "";
			html += "<ul>";
			$.each(data.data.rolelist, function(i, item) {
				var rid = item.rid;
				var name = item.name;
				if (i == 0) {
					initRole(rid);
				}
				html += "<li onclick='initRole(" + rid + ");'>" + name+ "</li>";
			});
			$.each(data.data.modulelist,function(i, item) {
				$("<option value='" + item.mid + "'>" + item.name+ "</option>").appendTo("#moduletype");
			});
			html += "</ul>";
			$("#role-authority-left-box").html(html);
			// 样式变更操作
			$('#role-authority-left-box  ul li:first-child').addClass('btn btn-sm btn-primary');
			$('#role-authority-left-box li').bind('click',function(event) {
				$("body").showLoading();
				initRoleModuleList();
				$('#role-authority-left-box li').removeClass('btn btn-sm btn-primary');
				$(this).addClass('btn btn-sm btn-primary');
			});
			initRoleModuleList();
		} else {
			$("#role-authority-left-box").html("");
		}
	}, "json");
}

// 初始化角色ID
function initRole(id) {
	$("#rid").val(id);
}

function initModuleType() {
	$("body").showLoading();
	initRoleModuleList();
}

// 初始化模块
function initRoleModuleList() {
	var pid = $("#moduletype").val();
	var rid = $("#rid").val();
	$.post(PATH + "/selectRoleMeunAuthListByPid.do", {
		id : pid,
		rid : rid,
		async : true, // 是否异步
	}, function(data) {
		if (data.ret == 1) {
			var modulelist = new Array();
			$("#moduleList").html("");
			$.each(data.data, function(i, item) {
				var mid = item.mid;
				var name = item.name;
				modulelist.push(mid);
				var html = "";
				html += "<dl>";
				html += '<dt>' + name + '</dt>';
				html += intiAuth(mid, item.authList);
				html += "</dl>";
				$("#moduleList").append(html);
			});
			$("#mid").val(modulelist.join(","));
			$("#moduleSave").show();
		} else {
			$("#moduleList").html("");
		}
		$("body").hideLoading();
	}, "json");
}
function intiAuth(mid, data) {
	var html = "";
	$.each(data,function(i, item) {
		var item_id = item.aid;
		var item_name = item.name;
		html += "<dd>";
		if (item.ischeck > 0) {
			html+='<div class="has-success"><div class="checkbox"><label>';
			html+='<input type="checkbox" checked="checked" id='+ mid+ '_'+ i+' name="auth'+ mid+'"  value=' + item_id + '>';
			html+=item_name;
			html+='</label></div></div>';
			//html += "<input style='padding-left:15px' type='checkbox'  checked='checked' id='"+ mid+ "_"+ i+ "' name='auth"+ mid+ "' value='" + item_id + "'>";
		} else {
			//html += "<input style='padding-left:15px' type='checkbox' id='"+ mid+ "_"+ i+ "' name='auth"+ mid+ "' value='" + item_id + "'>";
			html+='<div class="has-success"><div class="checkbox"><label>';
			html+='<input type="checkbox" id='+ mid+ '_'+ i+' name="auth'+ mid+'"  value=' + item_id + '>';
			html+=item_name;
			html+='</label></div></div>';
		}
		//html += "<label style='margin-top:-5px;padding-left:5px' for='"+ mid + "_" + i + "'>" + item_name + "</label>";
		html += '<hr style="margin:5px 0 5px 0;">';
		html += "</dd>";
	});
	return html;
}

function Save() {
	$("body").showLoading();
	var mid = $("#mid").val();
	var arrval = new Array();
	arrkey = mid.split(",");
	for (var i = 0; i < arrkey.length; i++) {
		var authority = new Array();
		$('input[name="auth' + arrkey[i] + '"]:checked').each(function() {// 遍历每一个名字为interest的复选框，其中选中的执行函数
			authority.push($(this).val());// 将选中的值添加到数组chk_value中
		});
		arrval.push(arrkey[i] + ":" + authority.join(","));
	}
	var roleauthority = arrval.join("#");
	var rid = $("#rid").val();
	$.ajax({
		async : true, // 是否异步
		type : 'Post',
		url : 'addSysRoleAuthority.do',
		data : 'rid=' + rid + "&list=" + roleauthority,
		dataType : 'json',
		success : function(data) {
			if (data.ret == 1) {
				top.layer.msg('数据保存成功.......', {time: 1000, icon:6});
			} else {
				top.layer.alert('数据保存失败....', {time: 2000,skin: 'layui-layer-lan',closeBtn: 0,anim: 4,icon : 5});
			}
			$("body").hideLoading();
		}
	});
}
